/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.vrangogym.web.admin;

import edu.chl.vrangogym.persistence.entities.User;
import edu.chl.vrangogym.persistence.entities.enums.Permissions;
import edu.chl.vrangogym.persistence.exception.NonexistentEntityException;
import edu.chl.vrangogym.persistence.impl.UserFacade;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

/**
 *
 * @author Anders
 */
@ManagedBean(name = "AdminUsersCtrl")
@RequestScoped
public class AdminUsersCtrlBean implements Serializable {
    @EJB
    private UserFacade userFacade;
    @ManagedProperty(value = "#{AdminUsers}")
    private AdminUsersBackingBean adminUsers;

    /** Creates a new instance of AddProductControllerBean */
    public AdminUsersCtrlBean() {
    }
    
    @PostConstruct
    public void postConstruct() {
    }

    public void permissionsChanged(ValueChangeEvent evt){
        User user = (User) evt.getComponent().getAttributes().get("user");
        user.setPermissions((Permissions) evt.getNewValue());
        try {
            userFacade.edit(user);
        } catch (NonexistentEntityException ex) {
            
        }
    }
    
    public void deleteUser(ActionEvent evt){
        User user = (User) evt.getComponent().getAttributes().get("user");
        try {
            userFacade.remove(user);
            adminUsers.updateUsers();
        } catch (NonexistentEntityException ex) {
            
        }
    }

    public AdminUsersBackingBean getAdminUsers() {
        return adminUsers;
    }

    public void setAdminUsers(AdminUsersBackingBean adminUsers) {
        this.adminUsers = adminUsers;
    }
    
}
